﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class UserMstDA : IUserMstDA
    {
        private string ADDRESS = "@ADDRESS";
        private string CITY = "@CITY";
        private string COUNTRY = "@COUNTRY";
        private string COUNTRY_CODE = "@COUNTRY_CODE";
        private string CREATED_BY = "@CREATED_BY";
        private string DEPARTMENT = "@DEPARTMENT";
        private string EMAIL = "@EMAIL";
        private string EXPIRE_DATE = "@EXPIRE_DATE";
        private string FAX = "@FAX";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string IS_DOMAIN_USER = "@IS_DOMAIN_USER";
        private string IS_LOCKED = "@IS_LOCKED";
        private string IS_LOGIN = "@IS_LOGIN";
        private string IS_WH_USER = "@IS_WH_USER";
        private string LOCK_DATE = "@LOCK_DATE";
        private string MOBILE = "@MOBILE";
        private string PASSWD = "@PASSWD";
        private string PASSWD_DATE = "@PASSWD_DATE";
        private string PHONE = "@PHONE";
        private string SQL_CHECK_USER_MST_ID_UNIQUE = " SELECT COUNT(1) FROM SYSADM.USER_MST WHERE USER_ID = @USER_ID ";
        private string SQL_DELETE_USER_MST = " DELETE FROM @_@USER_MST WHERE USER_ID = @USER_ID  AND SYSTEM_CREATED = 'N' ";
        private string SQL_INSERT_USER_MST = " INSERT INTO @_@USER_MST ( USER_ID, USER_NAME, PASSWD, PASSWD_DATE, EXPIRE_DATE, IS_LOGIN, IS_DOMAIN_USER, STATUS, IS_LOCKED, LOCK_DATE, CITY, STATE, ZIP, COUNTRY, COUNTRY_CODE, ADDRESS, MOBILE, PHONE, FAX, EMAIL, DEPARTMENT, IS_ACTIVE, SYSTEM_CREATED, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE,IS_WH_USER) VALUES ( @USER_ID, @USER_NAME, @PASSWD, @PASSWD_DATE, @EXPIRE_DATE, @IS_LOGIN, @IS_DOMAIN_USER, @STATUS, @IS_LOCKED, @LOCK_DATE, @CITY, @STATE, @ZIP, @COUNTRY, @COUNTRY_CODE, @ADDRESS, @MOBILE, @PHONE, @FAX, @EMAIL, @DEPARTMENT, @IS_ACTIVE, @SYSTEM_CREATED, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE(),@IS_WH_USER )  ";
        private string SQL_SELECT_ALL_USER_MST = " SELECT  ROWID, USER_ID, USER_NAME, PASSWD, PASSWD_DATE, EXPIRE_DATE, IS_LOGIN, IS_DOMAIN_USER, STATUS, IS_LOCKED, LOCK_DATE, CITY, STATE, ZIP, COUNTRY, COUNTRY_CODE, ADDRESS, MOBILE, PHONE, FAX, EMAIL, DEPARTMENT,IS_WH_USER, IS_ACTIVE, SYSTEM_CREATED, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@USER_MST ";
        private string SQL_UPDATE_USER_MST = " UPDATE @_@USER_MST SET USER_NAME = @USER_NAME, PASSWD = @PASSWD, PASSWD_DATE = @PASSWD_DATE, EXPIRE_DATE = @EXPIRE_DATE, IS_LOGIN = @IS_LOGIN, IS_DOMAIN_USER = @IS_DOMAIN_USER, STATUS = @STATUS, IS_LOCKED = @IS_LOCKED, LOCK_DATE = @LOCK_DATE, CITY = @CITY, STATE = @STATE, ZIP = @ZIP, COUNTRY = @COUNTRY, COUNTRY_CODE = @COUNTRY_CODE, ADDRESS = @ADDRESS, MOBILE = @MOBILE, PHONE = @PHONE, FAX = @FAX, EMAIL = @EMAIL, DEPARTMENT = @DEPARTMENT, IS_ACTIVE = @IS_ACTIVE, SYSTEM_CREATED = @SYSTEM_CREATED, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE(),IS_WH_USER=@IS_WH_USER  WHERE USER_ID = @USER_ID  ";
        private string STATE = "@STATE";
        private string STATUS = "@STATUS";
        private string SYSTEM_CREATED = "@SYSTEM_CREATED";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string USER_ID = "@USER_ID";
        private string USER_NAME = "@USER_NAME";
        private string ZIP = "@ZIP";

        public bool CheckUserMstIDUnique(string userMstID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", userMstID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_USER_MST_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckUserMstIDUnique(DataBase dataBase, DbTransaction tran, string userMstID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", userMstID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_USER_MST_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteUserMst(List<string> userMstID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < userMstID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", userMstID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_USER_MST.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteUserMst(DataBase dataBase, DbTransaction tran, List<string> userMstID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < userMstID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", userMstID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_USER_MST.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllUserMst(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_USER_MST.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllUserMst(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_USER_MST.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public UserMstInfo GetUserMstByID(string usermstID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_USER_MST.Replace("@_@", whLoginID) + " WHERE USER_ID = @USER_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", usermstID) };
            UserMstInfo userMstInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003;
                    userMstInfo = new UserMstInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["USER_ID"].ToString(), reader["USER_NAME"].ToString(), reader["PASSWD"].ToString(), (reader["PASSWD_DATE"].ToString() != "") ? DateTime.Parse(reader["PASSWD_DATE"].ToString()) : (CS00003 = new DateTime()), (reader["EXPIRE_DATE"].ToString() != "") ? DateTime.Parse(reader["EXPIRE_DATE"].ToString()) : (CS00003 = new DateTime()), reader["IS_LOGIN"].ToString(), reader["IS_DOMAIN_USER"].ToString(), Convert.ToInt16(reader["STATUS"]), reader["IS_LOCKED"].ToString(), (reader["LOCK_DATE"].ToString() != "") ? DateTime.Parse(reader["LOCK_DATE"].ToString()) : (CS00003 = new DateTime()), reader["CITY"].ToString(), reader["STATE"].ToString(), reader["ZIP"].ToString(), reader["COUNTRY"].ToString(), reader["COUNTRY_CODE"].ToString(), reader["ADDRESS"].ToString(), reader["MOBILE"].ToString(), reader["PHONE"].ToString(), reader["FAX"].ToString(), reader["EMAIL"].ToString(), reader["DEPARTMENT"].ToString(), reader["IS_WH_USER"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return userMstInfo;
        }

        public UserMstInfo GetUserMstByID(DataBase dataBase, DbTransaction tran, string usermstID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_USER_MST.Replace("@_@", whLoginID) + " WHERE USER_ID = @USER_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", usermstID) };
            UserMstInfo userMstInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                DateTime CS00003;
                userMstInfo = new UserMstInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["USER_ID"].ToString(), reader["USER_NAME"].ToString(), reader["PASSWD"].ToString(), (reader["PASSWD_DATE"].ToString() != "") ? DateTime.Parse(reader["PASSWD_DATE"].ToString()) : (CS00003 = new DateTime()), (reader["EXPIRE_DATE"].ToString() != "") ? DateTime.Parse(reader["EXPIRE_DATE"].ToString()) : (CS00003 = new DateTime()), reader["IS_LOGIN"].ToString(), reader["IS_DOMAIN_USER"].ToString(), Convert.ToInt16(reader["STATUS"]), reader["IS_LOCKED"].ToString(), (reader["LOCK_DATE"].ToString() != "") ? DateTime.Parse(reader["LOCK_DATE"].ToString()) : (CS00003 = new DateTime()), reader["CITY"].ToString(), reader["STATE"].ToString(), reader["ZIP"].ToString(), reader["COUNTRY"].ToString(), reader["COUNTRY_CODE"].ToString(), reader["ADDRESS"].ToString(), reader["MOBILE"].ToString(), reader["PHONE"].ToString(), reader["FAX"].ToString(), reader["EMAIL"].ToString(), reader["DEPARTMENT"].ToString(), reader["IS_WH_USER"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return userMstInfo;
        }

        public DataSet GetUserMstByQueryList(List<string> sqlWhere, UserMstQueryEntity usermstQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_USER_MST.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!usermstQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, usermstQuery.CurrentPage, usermstQuery.PageSize, usermstQuery.SortField, usermstQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetUserMstByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, UserMstQueryEntity usermstQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_USER_MST.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!usermstQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, usermstQuery.CurrentPage, usermstQuery.PageSize, usermstQuery.SortField, usermstQuery.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertUserMst(UserMstInfo usermstInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    SqlParameter[] paras = this.Set_UserMst_Parameters(usermstInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_USER_MST.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertUserMst(DataBase dataBase, DbTransaction tran, UserMstInfo userMstInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    SqlParameter[] paras = this.Set_UserMst_Parameters(userMstInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_USER_MST.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_UserMst_Parameters(UserMstInfo userMstInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { 
                new SqlParameter(this.USER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.USER_NAME, SqlDbType.VarChar, 40), new SqlParameter(this.PASSWD, SqlDbType.VarChar, 50), new SqlParameter(this.PASSWD_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.EXPIRE_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.IS_LOGIN, SqlDbType.VarChar, 1), new SqlParameter(this.IS_DOMAIN_USER, SqlDbType.VarChar, 1), new SqlParameter(this.STATUS, SqlDbType.SmallInt, 2), new SqlParameter(this.IS_LOCKED, SqlDbType.VarChar, 1), new SqlParameter(this.LOCK_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.CITY, SqlDbType.VarChar, 50), new SqlParameter(this.STATE, SqlDbType.VarChar, 20), new SqlParameter(this.ZIP, SqlDbType.VarChar, 20), new SqlParameter(this.COUNTRY, SqlDbType.VarChar, 30), new SqlParameter(this.COUNTRY_CODE, SqlDbType.VarChar, 20), new SqlParameter(this.ADDRESS, SqlDbType.VarChar, 200), 
                new SqlParameter(this.MOBILE, SqlDbType.VarChar, 20), new SqlParameter(this.PHONE, SqlDbType.VarChar, 20), new SqlParameter(this.FAX, SqlDbType.VarChar, 20), new SqlParameter(this.EMAIL, SqlDbType.VarChar, 50), new SqlParameter(this.DEPARTMENT, SqlDbType.VarChar, 50), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.IS_WH_USER, SqlDbType.VarChar, 1), new SqlParameter(this.SYSTEM_CREATED, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20)
             };
            paramArray[0].Value = userMstInfo.UserID;
            if (!string.IsNullOrEmpty(userMstInfo.UserName))
            {
                paramArray[1].Value = userMstInfo.UserName;
            }
            else
            {
                paramArray[1].Value = DBNull.Value;
            }
            paramArray[2].Value = userMstInfo.Passwd;
            DateTime CS00003 = new DateTime();
            if (userMstInfo.PasswdDate != CS00003)
            {
                paramArray[3].Value = userMstInfo.PasswdDate;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            CS00003 = new DateTime();
            if (userMstInfo.ExpireDate != CS00003)
            {
                paramArray[4].Value = userMstInfo.ExpireDate;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(userMstInfo.IsLogin))
            {
                paramArray[5].Value = userMstInfo.IsLogin;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            paramArray[6].Value = userMstInfo.IsDomainUser;
            paramArray[7].Value = userMstInfo.Status;
            paramArray[8].Value = userMstInfo.IsLocked;
            if (userMstInfo.LockDate != new DateTime())
            {
                paramArray[9].Value = userMstInfo.LockDate;
            }
            else
            {
                paramArray[9].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(userMstInfo.City))
            {
                paramArray[10].Value = userMstInfo.City;
            }
            else
            {
                paramArray[10].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(userMstInfo.State))
            {
                paramArray[11].Value = userMstInfo.State;
            }
            else
            {
                paramArray[11].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(userMstInfo.Zip))
            {
                paramArray[12].Value = userMstInfo.Zip;
            }
            else
            {
                paramArray[12].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(userMstInfo.Country))
            {
                paramArray[13].Value = userMstInfo.Country;
            }
            else
            {
                paramArray[13].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(userMstInfo.CountryCode))
            {
                paramArray[14].Value = userMstInfo.CountryCode;
            }
            else
            {
                paramArray[14].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(userMstInfo.Address))
            {
                paramArray[15].Value = userMstInfo.Address;
            }
            else
            {
                paramArray[15].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(userMstInfo.Mobile))
            {
                paramArray[0x10].Value = userMstInfo.Mobile;
            }
            else
            {
                paramArray[0x10].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(userMstInfo.Phone))
            {
                paramArray[0x11].Value = userMstInfo.Phone;
            }
            else
            {
                paramArray[0x11].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(userMstInfo.Fax))
            {
                paramArray[0x12].Value = userMstInfo.Fax;
            }
            else
            {
                paramArray[0x12].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(userMstInfo.Email))
            {
                paramArray[0x13].Value = userMstInfo.Email;
            }
            else
            {
                paramArray[0x13].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(userMstInfo.Department))
            {
                paramArray[20].Value = userMstInfo.Department;
            }
            else
            {
                paramArray[20].Value = DBNull.Value;
            }
            paramArray[0x15].Value = userMstInfo.IsActive;
            paramArray[0x16].Value = userMstInfo.IsWhUser;
            paramArray[0x17].Value = userMstInfo.SystemCreated;
            paramArray[0x18].Value = userMstInfo.CreatedBy;
            paramArray[0x19].Value = userMstInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateUserMst(UserMstInfo userMstInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    SqlParameter[] paras = this.Set_UserMst_Parameters(userMstInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_USER_MST.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateUserMst(DataBase dataBase, DbTransaction tran, UserMstInfo userMstInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    SqlParameter[] paras = this.Set_UserMst_Parameters(userMstInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_USER_MST.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

